<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Picture;
use Validator;

class PictureController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('admin.picture.picture-list');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.picture.picture-add1');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        //保存修改数据
        if($request->action == 'picture_add'&&$request->action) {
            $array = array('msg' => '修改失败!', 'status' => 'false');
            $form = $request->form;
            $data = [];
            foreach ($form as $k0 => $v0) {
                $name = $v0['name'];
                $data[$name] = $v0['value'];
            }

            $picture_data = [
                'picture_title' => $data['picture_title'] ? $data['picture_title'] :"",
                'picture_title2' => $data['picture_title2']? $data['picture_title2']:"",
//                'picture_cate' => $data['picture_cate'],
//                'picture_sort' => $data['picture_sort'],
                'picture_author' => $data['picture_author']?$data['picture_author']:"",
                'picture_come' => $data['picture_come']? $data['picture_come']:"",
                'picture_key' => $data['picture_key']?$data['picture_key']:"",
                'picture_dec' => $data['picture_dec']?$data['picture_dec']:"",
                'logo' => $data['logo']?$data['logo']:"",
                'logo_picutre' => $data['logo_picutre']?$data['logo_picutre']:"",
            ];
            $res1 = Picture::create($picture_data)->toSql();
dd($res1);

//            $article_data = [
//                'table' => $data['articletitle'],
//            ];

//            DB::beginTransaction();//开启事务需要使用ninoDB 类型数据库,一定不能忘了.
            try {
                $res1 = Picture::create($picture_data);
//                $res2 = Article::where('id', '=', $data['article_id'])->update($article_data);
                if ($res1) {
//                    DB::commit();
                    $array = array('msg' => '添加成功!', 'status' => 'success');
                }
            } catch (\Exception $e) {
//                DB::rollBack();
                dump('fail');
            }

            return json_encode($array);
        }



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //文件上传  上传excel后 文件后缀名为bin,因此不能使用该上传处理过程.
    public function upload(Request $request)
    {

//        $res = $request->file('picture')->store('picture/'.date('Y-m-d'),'public');
        $res = $request->picture->store('picture/'.date('Y-m-d'),'public');
//        dd($res);
        if( $res ){
            return ['status'=>'SUCCESS','file'=>'storage/'.$res];
        }
        return ['status'=>'ERROR','file'=>null];
    }

    //图片上传
    public function upload_picture_picture(Request $request)
    {

        dd($request->all());

        $res = $request->file('picture')->store('picture/'.date('Y-m-d'),'public');
//        dd($res);
        if( $res ){
            return ['status'=>'SUCCESS','file'=>'storage/'.$res];
        }
        return ['status'=>'ERROR','file'=>null];
    }

    //多图片上传
    public function picture_many(Request $request)
    {

//        dd($request->all());

        $res = $request->file->store('picture/'.date('Y-m-d'),'public');
//        dd($res);
        if( $res ){
            return ['status'=>'SUCCESS','file'=>'storage/'.$res];
        }
        return ['status'=>'ERROR','file'=>null];
    }
}
